﻿using System;

namespace Songhay.Extensions
{
    using Models;

    /// <summary>
    /// Extensions of <see cref="BlogEntry"/>.
    /// </summary>
    public static class BlogEntryExtensions
    {
        /// <summary>
        /// Converts the <see cref="BlogEntry" /> into a display item model.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="categoryGetter">The category getter.</param>
        /// <param name="descriptionGetter">The description getter.</param>
        /// <param name="tagGetter">The tag getter.</param>
        /// <param name="uriGetter">The URI getter.</param>
        public static DisplayItemModel ToDisplayItemModel(this BlogEntry entry,
            Func<BlogEntry, string> categoryGetter = null, Func<BlogEntry, string> descriptionGetter = null,
            Func<BlogEntry, string> tagGetter = null, Func<BlogEntry, Uri> uriGetter = null)
        {
            if (entry == null) return null;

            return new DisplayItemModel
            {
                ItemCategory = (categoryGetter == null) ? null : categoryGetter.Invoke(entry),
                Description = (descriptionGetter == null) ? null : descriptionGetter.Invoke(entry),
                DisplayText = entry.Title,
                ItemName = entry.Slug,
                ResourceIndicator = (uriGetter == null) ? null : uriGetter.Invoke(entry),
                Tag = (tagGetter == null) ? null : tagGetter.Invoke(entry)
            };
        }
    }
}
